using System;
using System.Collections.Generic;
using Commons.Models.Settings.Dtos.TradeValues;
using Commons.Models.TradeValues;

namespace Commons.Models.Settings.Mappers.TradeValues
{
	public class TradeValuesMapper : IMapper<MultipleTradeValueDto, ITradeValue>
	{
		protected static Dictionary<Type, IMapper<TradeValueDto, ITradeValue>> mappers;

		static TradeValuesMapper()
		{
			mappers = new Dictionary<Type, IMapper<TradeValueDto, ITradeValue>>();
			mappers.Add(typeof(ProbabilityTradeValueDto), new ProbabilityTradeValueMapper());
			mappers.Add(typeof(UserStatTradeValueDto), new UserStatTradeValueMapper());
			}
		
		public ITradeValue Map(MultipleTradeValueDto config)
		{
			if (config == null || config.GetTradeValues() == null || config.GetTradeValues().Count == 0)
			{
				return null;
			}

			List<ITradeValue> tradeValues = new List<ITradeValue>();

			foreach (TradeValueDto dto in config.GetTradeValues())
			{
				tradeValues.Add(mappers[dto.GetType()].Map(dto));
			}

			if (tradeValues.Count > 1)
			{
				return new MultipleTradeValue(tradeValues);
			}
			else if (tradeValues.Count == 1)
			{
				return tradeValues[0];
			}
			else
			{
				return null;
			}
		}
	}
}

